/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.hadoop.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2013-8-12 上午09:49:33
 */

public class FileCopyWithProgress {
	public static void main(String[] args) {
		String localStr = "E:\\chenzehe\\mvnrepo.rar";
		String hdfsStr = "hdfs://192.168.0.2:49000/user/hadoop/mvnrepo.rar";
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(localStr));
			Configuration conf = new Configuration();
			FileSystem fs = FileSystem.get(URI.create(hdfsStr), conf);
			out = fs.create(new Path(hdfsStr), new Progressable() {
				public void progress() {
					System.out.print("*");
				}
			});
			IOUtils.copyBytes(in, out, 4096, true);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			IOUtils.closeStream(in);
			IOUtils.closeStream(out);
		}
	}
}
